package greedy;

import java.util.Arrays;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class candy {
    public int candy(int[] ratings) {
        if (ratings.length == 1){
            return 1;
        }
        int[] candyVec = new int[ratings.length];
        candyVec[0] = 1;
        for (int i = 1; i < ratings.length; i++) {
            candyVec[i] = (ratings[i] > ratings[i - 1]) ? candyVec[i - 1] + 1 : 1;
        }

        for (int i = ratings.length - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                candyVec[i] = Math.max(candyVec[i], candyVec[i + 1] + 1);
            }
        }
        return Arrays.stream(candyVec).sum();
    }
}
